package api_day13;
/*
    线程池测试类
 */
import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        Runnable task = new Runnable() {
            @Override
            public void run() {
                Thread t = Thread.currentThread();
                for (int i = 0; i < 5; i++) {
                    System.out.println(t.getName() + ":" + i * 2);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        Callable<String> call = new Callable<>() {
            @Override
            public String call() {
                Thread t = Thread.currentThread();
                for (int i = 0; i < 3; i++) {
                    System.out.println(t.getName() + ":" + i * 2);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                return t.getName();
            }
        };
        /*Future<?> submit1 = pool.submit(task);
        Future<?> submit2 = pool.submit(task,1);
        pool.submit(task);
        pool.submit(task);
        System.out.println(submit1.get());
        System.out.println(submit2.get());*/
        /*pool.execute(task);
        pool.execute(task);
        pool.execute(task);
        pool.execute(task);*/
        Future<String> f1 = pool.submit(call);
        pool.submit(call);
        pool.submit(call);
        pool.submit(call);
        System.out.println(f1.get());
        pool.shutdown();
    }
}
